<template>
  <div class="container">
    <div class="login-wrapper">
      <h2 class="header">登 录</h2>
      <div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="name">
            <el-input
              type="text"
              v-model="ruleForm.name"
              placeholder="请输入手机号或用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="checked">
            <el-checkbox-group v-model="ruleForm.checked">
              <el-checkbox
                label="我已阅读并且同意mac用户使用协议"
                name="checked"
              ></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item>
            <div class="btn1">
              <el-button
                class="btn"
                type="primary"
                @click="submitForm('ruleForm')"
                >登录</el-button
              >
              <router-link to="/register" class="register"
                >没有账号，去注册</router-link
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // let validatePass = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入密码'))
    //   } else {
    //     if (this.ruleForm.checkPass !== '') {
    //       this.$refs.ruleForm.validateField('checkPass')
    //     }
    //     callback()
    //   }
    // }
    return {
      ruleForm: {
        checked: [], // 用户是否同意用户使用协议
        pass: "", // 密码
        name: "", // 用户
      },
      rules: {
        pass: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "密码长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        name: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        checked: [
          {
            type: "array",
            required: true,
            message: "请确认阅读",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 验证表单并登录
    submitForm(formName) {
      // 进行验证
      this.$refs[formName].validate((valid) => {
        // 如果验证成功
        if (valid) {
          // 登录需要的参数
          let params = { name: this.ruleForm.name, pwd: this.ruleForm.pass };

          // 发送登录请求
          this.$http.userApi.login(params).then((res) => {
            console.log(res);
            if (res.data.code == 200) {
              sessionStorage.setItem("user", JSON.stringify(res.data.data));
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.push("/");
            } else {
              this.$message(res.data.msg);
            }
          });
        }
      });
    },
    // resetForm(formName) {
    //   this.$refs[formName].resetFields()
    // },
    zuoyi(event) {
      event.currentTarget.className = "zuoyi";
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form-item {
  color: #eee;
}
.container {
  height: 100vh;
  border: none;
  // background: url("../assets/images/1.png");
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
  > .login-wrapper {
    // 背景色
    background-color: #fff;
    width: 400px;
    height: 600px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
    // 圆角
    border-radius: 15px;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    > .header {
      font-size: 38px;
      font-weight: bold;
      text-align: center;
      line-height: 150px;
    }

    > div {
      padding: 10%;
      padding-left: 0%;
    }
  }
}
.dl {
  border: 1px #333 solid;
}
.zc {
  border: 1px #333 solid;
}
.el-button {
  margin-left: 25%;
}
span {
  color: white;
}
h2 {
  user-select: none;
}
.dlzc {
  display: flex;
}
.btn1 {
  display: flex;
  flex-direction: column;
}
.btn {
  margin-left: -5px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  border-color: #fff;
}
.register {
  margin-left: 50px;
  color: lightblue;
}
</style>
